﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Action.ExpressionPlugin
{
    public delegate IMetaDataContent MetadataCacheManager(string metaDataID);
    public delegate IMetaData MetadataEntityCacheManager(string metaDataID);
    public class BaseProvider
    {
        public event MetadataCacheManager GetMetadata = null;
        public event MetadataEntityCacheManager GetMetadataEntity = null;


        public bool IsDev
        {
            get
            {
                if (GetMetadata == null)
                {
                    return true;
                }
                return false;
            }
        }

        public RefSchemaExprAPI RefSchemaExprUtil
        {
            get
            {
                var api = RefSchemaExprAPI.GetInstance;
                api.IsDev = IsDev;
                return api;
            }
        }

        public IMetaDataContent GetMetaDataContentByType(string id, ProviderMetadataType pType,string projectBranchID ="")
        {
            if (GetMetadata != null)
            {
                var meta = GetMetadata(id);
                switch (pType)
                {
                    case ProviderMetadataType.DO:
                        return meta as DataObject;
                    case ProviderMetadataType.EO:
                        return meta as EntityObject;
                    case ProviderMetadataType.CDO:
                        return meta as CompositeDataObject;
                    case ProviderMetadataType.DQ:
                        return meta as DataQuery;
                    case ProviderMetadataType.Action:
                        return meta as ActionMetaData;
                }
                return null;
            }
            else
            {

                switch (pType)
                {
                    case ProviderMetadataType.DO:
                        //return new T.FAS.Runtime.DataModel.Plugin.DataObjectManager().GetMetaDataContent(id) as DataObject;
                        return new DevMetadataService().GetMetadata(id, projectBranchID) as DataObject;
                    case ProviderMetadataType.EO:
                        //return new T.FAS.Runtime.DataModel.Plugin.EntityObjectManager().GetMetaDataContent(id) as EntityObject;
                        return new DevMetadataService().GetMetadata(id, projectBranchID) as EntityObject;
                    case ProviderMetadataType.CDO:
                        //return new T.FAS.Runtime.DataModel.Plugin.CompositeDataObjectManager().GetMetaDataContent(id) as CompositeDataObject;
                        return new DevMetadataService().GetMetadata(id, projectBranchID) as CompositeDataObject;
                    case ProviderMetadataType.DQ:
                        //return new T.FAS.Runtime.DataModel.Plugin.DataQueryManager().GetMetaDataContent(id) as DataQuery;
                        return new DevMetadataService().GetMetadata(id, projectBranchID) as DataQuery;
                    case ProviderMetadataType.Action:
                        //return new ActionMetaDataManager().GetMetaDataContent(id);
                        return new DevMetadataService().GetMetadata(id, projectBranchID) as ActionMetaData;
                }
                return null;
            }
        }

        public IMetaData GetMetaData(string id, string projectBranchID = "")
        {
            if (GetMetadata != null)
            {
                var meta = GetMetadataEntity(id);
                return meta;
            }
            else
            {
                return DevMetadataService.GetMetadataEntity(id, projectBranchID);
            }
        }
    }
    public enum ProviderMetadataType
    {
        DO,
        EO,
        CDO,
        DQ,
        Action
    }
}
